//
//  XHFirstMenuCell.m
//  elxc
//
//  Created by fengyn on 16/3/14.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHFirstMenuCell.h"
#import "HMSegmentedControl.h"

@implementation XHFirstMenuCell

- (void)awakeFromNib {
    // Initialization code
    // Segmented control with scrolling
    self.backgroundColor=[UIColor groupTableViewBackgroundColor];
    [self.ui_segmentedControl removeFromSuperview];
    HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"话费直充",@"智能缴费 ",@"流量直充",@"流量屯包"]];
//    segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
    segmentedControl.backgroundColor=[UIColor whiteColor];
    segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
    segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    segmentedControl.selectionIndicatorColor=[ColorManager colorWithMain];
    segmentedControl.selectionIndicatorHeight=3;
    [segmentedControl setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
        NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [ColorManager colorWithHexString:@"#1D1D1D" alpha:0.5],NSFontAttributeName : [UIFont systemFontOfSize:14]}];
        if (selected) {
            attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [ColorManager colorWithMain],NSFontAttributeName : [UIFont systemFontOfSize:15]}];
        }
        return attString;
    }];
    [segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
    [self.contentView addSubview:segmentedControl];
    self.ui_segmentedControl=segmentedControl;
    /*
    [self.ui_segmentedControl setSectionTitles:@[@"话费直充",@"智能缴费 ",@"流量直充",@"流量屯包"]];
    self.ui_segmentedControl.backgroundColor=[UIColor whiteColor];
    self.ui_segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 20, 0, 20);
    [self.ui_segmentedControl setSelectionStyle:HMSegmentedControlSelectionStyleFullWidthStripe];
    [self.ui_segmentedControl setSelectionIndicatorLocation:HMSegmentedControlSelectionIndicatorLocationNone];
    [self.ui_segmentedControl setSelectionIndicatorColor:[UIColor blueColor]];
    
    [self.ui_segmentedControl setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
        NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [UIColor colorWithWhite:0 alpha:0.5],NSFontAttributeName : [UIFont systemFontOfSize:15]}];
        if (selected) {
            attString = [[NSAttributedString alloc] initWithString:title attributes:@{NSForegroundColorAttributeName : [UIColor colorWithWhite:0 alpha:0.8],NSFontAttributeName : [UIFont systemFontOfSize:18]}];
        }
        return attString;
    }];
    [self.ui_segmentedControl addTarget:self action:@selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
     */
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.ui_segmentedControl.frame=CGRectMake(0, 0, self.contentView.frame.size.width, 45);
}

- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl {
    NSLog(@"Selected index %ld (via UIControlEventValueChanged)", (long)segmentedControl.selectedSegmentIndex);
    if ([self.delegate respondsToSelector:@selector(menuDidSelectedItemIndex:)]) {
        [self.delegate menuDidSelectedItemIndex:segmentedControl.selectedSegmentIndex];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
